///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.components.videoPlayer {
	import mx.collections.ArrayCollection;
	import mx.collections.CursorBookmark;
	import mx.collections.IViewCursor;
	import mx.events.CollectionEvent;
	
	import sugarcookie.error.ApplicationError;
	import sugarcookie.components.videoPlayer.vo.PlayListItem;

	public class PlayList {
		private var _list:ArrayCollection;
		private var _cursor:IViewCursor;
		private var _autoStart:Boolean;
		private var _loop:Boolean;
		
		public function PlayList(autoStart:Boolean=true,loop:Boolean=false) {
			_autoStart = autoStart;
			_loop = loop;
			_list = new ArrayCollection();
			_list.addEventListener(CollectionEvent.COLLECTION_CHANGE,listUpdate);
		}
		
		public function addToQueue(item:PlayListItem):void {
			//TODO - add position for updating playlist
			_list.addItem(item);
		}
		
		public function get autoStart():Boolean { return _autoStart; }
		
		public function get current():PlayListItem {
			if (_list.length==0) throw new ApplicationError("No PlayListItems have been added to the play list!");
			
			return PlayListItem(_cursor.current);
		}
		
		public function next():Boolean {
			var status:Boolean = true;
			if (!_cursor.moveNext()) {
				status = false;
				_cursor.seek(CursorBookmark.FIRST);
			}
			return status;	
		}
		
		public function previous():Boolean {
			var status:Boolean = true;
			if (!_cursor.movePrevious()) {
				status = false;
				_cursor.seek(CursorBookmark.FIRST);
			}
			return status;
		}
		
		public function seek():void {
			
		}
		
		private function listUpdate(evt:CollectionEvent):void {
			if (evt.kind=="add" && evt.location==0) {
				_list.removeEventListener(CollectionEvent.COLLECTION_CHANGE,listUpdate);
				_cursor = _list.createCursor();
			}
		}
	}
}